package com.tkjy.controller;

import com.tkjy.pojo.Account;
import com.tkjy.pojo.PageTransaction;
import com.tkjy.pojo.Transaction;
import com.tkjy.service.AccountService;
import com.tkjy.utils.Md5Util;
import com.tkjy.utils.R;
import com.tkjy.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@ResponseBody
@RequestMapping("/acc")
public class AccountController {
    @Autowired
    private AccountService accountService;

    @PostMapping("/getAccount")
    public R getAccount(@RequestBody Account account) {
        Account acc = accountService.selectAccount(account);
        if (acc.getPassword().equals(Md5Util.encrypt(account.getPassword())) && acc.getCardno().equals(account.getCardno())) {
            String token = UUID.randomUUID().toString().replaceAll("-", "");
            RedisUtil redisUtil = new RedisUtil();
            // 添加过期时间（单位：秒）
            redisUtil.setKey(token, token, 1800);
            return R.ok(0, "登录成功", acc, token);
        } else {
            return R.fail(1, "密码错误", null); // 统一错误提示格式
        }
    }

    @PostMapping("/yue")
    public R selectCarDno(@RequestBody Account account) {
        Account acc = accountService.selectAccount(account);
        if (acc.getPassword().equals(account.getPassword()) && acc.getCardno().equals(account.getCardno())) {
            return R.ok(acc);
        } else {
            return R.fail();
        }
    }

    @PostMapping("/updatePassword")
    public R updatePassword(@RequestBody Map<String, Object> data) {
        String password = (String) data.get("NewPassword");
        String cardno = (String) data.get("cardno");
        boolean b = accountService.updatePassword(password, cardno);
        if (b) {
            return R.ok();
        } else {
            return R.fail(1, "密码修改失败!!!", null);
        }
    }

    @PostMapping("/selectTransaction")
    public R selectTransaction(@RequestBody Map<String, Object> data) {
        String firstDate = (String) data.get("firstDate");
        String lastDate = (String) data.get("lastDate");
        String cardno = (String) data.get("cardno");
        Integer pageNo = (Integer) data.get("pageNo");
        Integer pageSize = (Integer) data.get("pageSize");
        PageTransaction pageTransaction = accountService.selectTransaction(firstDate, lastDate, cardno, pageNo, pageSize);
        if (pageTransaction != null) {
            return R.ok(pageTransaction);
        } else {
            return R.fail();
        }
    }

    @PostMapping("/updateMoney")
    public R updateMoney(@RequestBody Map<String, Object> data) {
        Double money = Double.valueOf((String) data.get("money"));
        String carDno = (String) data.get("cardno");
        String newCarDno = (String) data.get("newcardno");
        boolean flag = accountService.updateMoney(money, carDno, newCarDno);
        if (flag) {
            return R.ok();
        } else {
            return R.fail();
        }
    }
}

